package operation;
import book.BookList;

import book.Book;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: wangj
 * Date: 2022-02-28
 * Time: 23:42
 */
public class DeleteOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        Scanner scn=new Scanner(System.in);
        System.out.println("请输入要删除图书的名字:");
        String name= scn.nextLine();
        int size=bookList.getUsedSize();
        int index=-1;
        for (int i = 0; i < size; i++) {
            Book book=bookList.getBookList(i);
            if(book.getName().equals(name)){
                index=i;
                break;
            }
        }

        if(index==-1){
            System.out.println("没有你要删除的图书！");
        }//这里开始删除
        else {
            for (int i = index; i < size-1; i++) {
                //bookList[i]=bookList[i+1]
                Book book=bookList.getBookList(i+1);
                bookList.setBookList(book, i);
            }
            bookList.setUsedSize(size-1);
            System.out.println("删除成功！");
        }
    }
}
